<?php
class Weixin{
	
	public $AppID = '';
	public $AppSecret = '';
	public $ci;
	
	public function __construct()
	{
		$this->ci =& get_instance();
		$this->AppID = config_item('AppID');
		$this->AppSecret = config_item('AppSecret');
	}
	
	public function get_token()
	{
		$this->ci->load->driver('cache');
		if($access_token = $this->ci->cache->file->get('access_token'))
		{
			 return $access_token;
		}
		$url = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid='.$this->AppID.'&secret='.$this->AppSecret;
		$res = curl($url);
		$res = json_decode($res,true);
		if(isset($res['access_token']))
		{
			$this->ci->cache->file->save('access_token', $res['access_token'], 120);
			return $res['access_token'];
		}
		else 
		{
			log_message('error', print_r($res,true));
		}
	}
}